package com.lrabbit.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
public class UUIDUtil {

    @Value("${server.workId}")
    private static long workId = 1;

    /**
     * 获取一个随机的UUID，去掉短线"-"
     *
     * @return UUID
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 获取雪花算法生成的ID
     *
     * @return 雪花算法生成的ID
     */
    public static long getSnowflakeId() {

        // 创建Snowflake对象，传入工作机器ID
        Snowflake snowflake = IdUtil.getSnowflake(workId);
        return snowflake.nextId();
    }

    /**
     * 获取雪花算法生成的ID的字符串形式
     *
     * @return 雪花算法生成的ID的字符串形式
     */
    public static String getSnowflakeStrId() {
        Snowflake snowflake = IdUtil.getSnowflake(workId);
        long id = snowflake.nextId();
        return String.valueOf(id);
    }

}
